package it.okkam.longwell.ui.command;

import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.Vector;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletContext;

import org.apache.log4j.Logger;
import org.apache.velocity.VelocityContext;

import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.tdb.TDBFactory;
import com.hp.hpl.jena.tdb.store.Hash;
import com.hp.hpl.jena.vocabulary.RDF;

import edu.mit.simile.longwell.LongwellServlet;
import edu.mit.simile.longwell.Profile;
import edu.mit.simile.longwell.corpus.Corpus;
import edu.mit.simile.longwell.corpus.TDBConnector;
import edu.mit.simile.longwell.login.CheckLogin;
import edu.mit.simile.longwell.query.Query;
import edu.mit.simile.longwell.query.Restriction;
import edu.mit.simile.longwell.schema.IStructuredSchema;
import edu.mit.simile.longwell.schema.impl.StructuredSchema;
import edu.mit.simile.longwell.ui.FlairMessage;
import edu.mit.simile.longwell.ui.FlairURL;
import edu.mit.simile.longwell.ui.InjectionManager;
import edu.mit.simile.longwell.ui.command.CommandBase;



public class SetDatasetCommand extends CommandBase {
	final static private Logger s_logger = Logger.getLogger(SetDatasetCommand.class);
	CheckLogin check=null;
	final protected String m_template;
	public static boolean userInput = false;
	VelocityContext vcContext = null;
	private static String[] listaComuni;
	private Profile m_profile;
	private HashMap<String, String> hash;
	/**
	 * @param injectionManager
	 */
	public SetDatasetCommand(InjectionManager injectionManager, String template) {
		super(injectionManager);
		m_template = template;
	}

	/* (non-Javadoc)
	 * @see edu.mit.simile.flair.IFlairCommand#execute(edu.mit.simile.flair.FlairMessage)
	 */

	public void execute(FlairMessage msg) throws ServletException {
		s_logger.info("execute select comuni");
		listaComuni = null;
		listaComuni = msg.m_request.getParameterValues("comuneName");
		msg.m_request.setAttribute("listaComuni", listaComuni);
		HttpSession ses = msg.m_request.getSession();
		ses.setAttribute("listaComuni", listaComuni);
		hash = new HashMap<String, String>();
		//m_profile.getModel().removeAll();
		
		
	//	m_corpus.removeAll();
		HttpSession session = msg.m_request.getSession();
		Dataset ds = null;
		if (listaComuni!=null){
			for (int i=0; i<listaComuni.length;i++){			
				//m_corpus.add(m_dataset.get(ds).getNamedModel(values[i]));
				//m_profile.addData(m_dataset.get(ds).getNamedModel(values[i]));
				s_logger.info("Select comune=" +listaComuni[i]);						
				session.setAttribute(session.getId(), listaComuni);
				ds =TDBFactory.assembleDataset(LongwellServlet.getAssemblerDirectory()+"/"+listaComuni[i]+"-assembler.ttl");
			}
			Model model = 	ds.getDefaultModel();
			try {
				//msg.m_query.addRestriction("edu.mit.simile.longwell.query.project.SchemaBasedProjector", "",  "edu.mit.simile.longwell.query.bucket.DistinctValueBucketer", "rhttp://www.semanticweb.org/ontologies/2011/9/Rivela-Flussi.owl#");
				
				//msg.m_response.sendRedirect(new FlairURL(msg).changeCommandQuery("selectGrafi","").toURLString());
				msg.m_response.sendRedirect(new FlairURL(msg).changeCommandQuery("selectLoadingType","").toURLString());
//				msg.getProfile(session.getId().toString()).addData(model);
//				Set schemas = getSchemas(msg);
//				
//				Iterator iter= schemas.iterator();
//				while (iter.hasNext()){
//					IStructuredSchema o = (IStructuredSchema) iter.next();
//					String schema = o.getURI();
//					msg.m_query.addRestriction("edu.mit.simile.longwell.query.project.SchemaBasedProjector", "",  "edu.mit.simile.longwell.query.bucket.DistinctValueBucketer", schema);
//				}
//				msg.m_response.sendRedirect(new FlairURL(msg).changeCommandQuery("browse-default-model","").toURLString());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else{
			try {
				msg.m_response.sendRedirect(new FlairURL(msg).changeCommandQuery("selectDataset","").toURLString());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	
	protected Set getSchemas(FlairMessage msg) {
		SortedSet sSchemas = new TreeSet(new Comparator() {
			public boolean equals(Object obj) {
				return false;
			}

			public int compare(Object o1, Object o2) {
				IStructuredSchema ss1 = (IStructuredSchema) o1;
				IStructuredSchema ss2 = (IStructuredSchema) o2;

				return ss1.getURI().compareTo(ss2.getURI());
			}
		});

		sSchemas.addAll(msg.getProfileFromSession().getSchemaModel().getStructuredSchemas());
		return sSchemas;
	}
	
	public static final String[] getListaComuni(){
		return listaComuni;
	}
	
}